home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / bin / setupcon < prev    next >
Encoding:
Text File  |  2009-04-08  |  8.6 KB  |  332 lines

  1. #!/bin/sh
  2.  
  3. #     setupcon -- setup the font and keyboard on the Linux console
  4. #     Copyright ¬© 1999,2000,2001,2002,2003,2006 Anton Zinoviev
  5.  
  6. #     This program is free software; you can redistribute it and/or modify
  7. #     it under the terms of the GNU General Public License as published by
  8. #     the Free Software Foundation; either version 2 of the License, or
  9. #     (at your option) any later version.
  10.  
  11. #     This program is distributed in the hope that it will be useful,
  12. #     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #     GNU General Public License for more details.
  15.  
  16. #     If you have not received a copy of the GNU General Public License
  17. #     along with this program, write to the Free Software Foundation, Inc.,
  18. #     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. ###########################################################################
  21.  
  22.  
  23. # The same as /usr/bin/which - in order to make "which" available before
  24. # /usr is mounted
  25. which () {
  26.     local IFS
  27.     IFS=:
  28.     for i in $PATH; do
  29.     if [ -x "$i/$1" ]; then
  30.         echo "$i/$1"
  31.         return 0
  32.     fi
  33.     done
  34.     return 1
  35. }
  36.  
  37. while [ "$1" ]; do
  38.     case "$1" in
  39.     -k|--keyboard-only)
  40.         keyboard_only=yes
  41.         ;;
  42.     -f|--font-only)
  43.         font_only=yes
  44.         ;;
  45.     -v|--verbose)
  46.         verbose_option=yes
  47.         ;;
  48.     --force)
  49.         force=yes
  50.         ;;
  51.     --save)
  52.         save=yes
  53.         ;;
  54.     --save-only)
  55.         force=yes
  56.         save=yes
  57.         save_only=yes
  58.         ;;
  59.     -h|--help)
  60.         cat >&2 <<EOF
  61. Usage: setupcon [OPTION] [VARIANT]
  62. Sets up the font and the keyboard on Linux console.
  63.  
  64.   -k, --keyboard-only  setup the keyboard only, do not setup the font
  65.   -f, --font-only      setup the font only, do not setup the keyboard
  66.       --force          do not check whether we are on the console
  67.   -v, --verbose        explain what is being doing, try it if s.t. goes wrong
  68.       --save           copy the font and the ACM in /etc/console-setup,
  69.                          update /etc/console-setup/boottime.kmap.gz
  70.       --save-only      only save; don't setup keyboard/font immediately
  71.                          (implies --force)
  72.   -h, --help           display this help and exit
  73.  
  74. If VARIANT is not specified setupcon looks for the configuration files
  75. (in this order) ~/.console-setup and /etc/default/console-setup.  When
  76. a VARIANT is specified then setupcon looks for the configuration files
  77. ~/.console-setup.VARIANT and /etc/default/console-setup.VARIANT.
  78. EOF
  79.         exit 0
  80.         ;;
  81.     -*)
  82.         echo "setupcon: Unrecognised option $1" >&2
  83.         exit 1
  84.         ;;
  85.     *)
  86.         if [ -z "$VARIANT" ]; then
  87.         VARIANT="$1"
  88.         else
  89.         echo "setupcon: Two variants specified: $VARIANT and $1" >&2
  90.         exit 1
  91.         fi
  92.         ;;
  93.     esac
  94.     shift
  95. done
  96.  
  97. if [ "$VARIANT" ]; then
  98.     USER_CONFIG=${HOME}/.console-setup."$VARIANT"
  99.     MAIN_CONFIG=/etc/default/console-setup."$VARIANT"
  100. else
  101.     USER_CONFIG=${HOME}/.console-setup
  102.     MAIN_CONFIG=/etc/default/console-setup
  103. fi
  104.  
  105. if [ -f "$USER_CONFIG" ]; then
  106.     CONFIG="$USER_CONFIG"
  107.     save=
  108. elif [ -f "$MAIN_CONFIG" ]; then
  109.     CONFIG="$MAIN_CONFIG"
  110. else
  111.     echo "setupcon: None of $MAIN_CONFIG nor $USER_CONFIG exists." >&2
  112.     exit 1
  113. fi
  114.  
  115. . "$CONFIG"
  116.  
  117. if [ -d /lib/debian-installer ]; then
  118.     CHARMAP=UTF-8
  119. fi
  120.  
  121. if [ "$verbose_option" = yes ]; then
  122.     VERBOSE_OUTPUT=yes
  123. fi
  124. if [ "$VERBOSE_OUTPUT" = yes ]; then
  125.     verbose=''
  126. else
  127.     verbose='>/dev/null 2>&1'
  128. fi
  129.  
  130. if which ckbcomp-mini >/dev/null && [ "$CHARMAP" != UTF-8 ]; then
  131.     CHARMAP=UTF-8
  132.     if [ "$VERBOSE_OUTPUT" = yes ]; then
  133.     echo Only UTF-8 is supported by console-setup-mini
  134.     fi
  135. fi
  136.  
  137. if [ "$force" != yes ]; then
  138.     case `readlink /proc/self/fd/2` in
  139.     /dev/tty[0-9]*|/dev/vc/[0-9]*|/dev/console)
  140.         ;;
  141.     *)
  142.         if [ "$VERBOSE_OUTPUT" = yes ]; then
  143.         echo We are not on the Linux console, exiting.
  144.         fi
  145.         exit 0 
  146.         ;;
  147.     esac
  148. fi
  149.  
  150. #-----------------------#
  151. #       OUTPUT          #
  152. #-----------------------#
  153.  
  154. if [ "$keyboard_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  155.     for console in $ACTIVE_CONSOLES; do
  156.     [ -w $console ] || continue
  157.     # Setup unicode/non-unicode mode
  158.     if [ "$save_only" != yes ]; then
  159.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM$CHARMAP" ]; then
  160.         /bin/echo -n -e '\033%G' >$console
  161.         else
  162.         /bin/echo -n -e '\033%@' >$console
  163.         fi
  164.     fi
  165.  
  166.     # Load the font
  167.     if [ ! -f "$FONT" ]; then
  168.         for dir in \
  169.         /etc/console-setup \
  170.         /usr/local/share/consolefonts \
  171.         /usr/share/consolefonts
  172.         do
  173.           if [ -f "$dir/$CODESET-$FONTFACE$FONTSIZE.psf.gz" ]; then
  174.           FONT="$dir/$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  175.           case "$FONTSIZE" in
  176.               *x*)
  177.               bigfont=yes
  178.               ;;
  179.               *)
  180.               bigfont=no
  181.               ;;
  182.           esac
  183.           break
  184.           fi
  185.         done
  186.     fi
  187.     if [ -f "$FONT" ]; then
  188.         if \
  189.         [ "$save" = yes ] \
  190.         && [ "${FONT%/*}" != /etc/console-setup ]
  191.         then
  192.         cp "$FONT" /etc/console-setup/
  193.         fi
  194.     else
  195.         if which ckbcomp-mini >/dev/null; then
  196.         FONT=$(echo `ls /usr/share/consolefonts/$CODESET-*.psf.gz \
  197.                                  2>/dev/null`)
  198.         FONT=${FONT%% *}
  199.         if [ -z "$FONT" ]; then
  200.             FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  201.         fi
  202.         else
  203.         FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  204.         fi
  205.         case "$FONTSIZE" in
  206.         *x*)
  207.             bigfont=yes
  208.             ;;
  209.         *)
  210.             bigfont=no
  211.             ;;
  212.         esac
  213.     fi
  214.     if [ "$save_only" != yes ]; then
  215.         if which consolechars >/dev/null; then
  216.         if [ "$bigfont" = yes ]; then
  217.             echo "setupcon: The consolechars utility from the console-setup font can load only fonts with 8 pixel width matrix.  Please install the setfont utility from the kbd package." >&2
  218.         fi
  219.         eval consolechars -v --tty=$console -f "$FONT" $verbose
  220.         elif which setfont >/dev/null; then
  221.         eval setfont -v -C $console "$FONT" $verbose
  222.         fi
  223.     fi
  224.  
  225.     # Load the ACM
  226.     if [ ! -f "$ACM" ]; then
  227.         for dir in \
  228.         /etc/console-setup \
  229.         /usr/local/share/consoletrans \
  230.         /usr/share/consoletrans
  231.         do
  232.           if [ -f "$dir/$CHARMAP.acm.gz" ]; then
  233.           ACM="$dir/$CHARMAP.acm.gz"
  234.           break
  235.           fi
  236.         done
  237.     fi
  238.     if [ -f "$ACM" ]; then
  239.         if \
  240.         [ "$save" = yes ] \
  241.         && [ "${ACM%/*}" != /etc/console-setup ]
  242.         then
  243.         cp "$ACM" /etc/console-setup/
  244.         fi
  245.     else
  246.         ACM="$CHARMAP.acm.gz"
  247.     fi
  248.     if [ "$save_only" != yes ] && [ "$CHARMAP" != UTF-8 ]; then
  249.         if which consolechars >/dev/null; then
  250.         eval consolechars -v --tty=$console --acm "$ACM" $verbose
  251.         elif which setfont >/dev/null; then
  252.         eval setfont -v -C $console -m "$ACM" $verbose
  253.         fi        
  254.     fi
  255.     done
  256. fi
  257.  
  258. #-----------------------#
  259. #        INPUT          #
  260. #-----------------------#
  261.  
  262. if [ "$font_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  263.     # On Mac PPC machines, we may need to set kernel vars first.  We need
  264.     # to mount /proc to do that, but we need it set up before sulogin may
  265.     # be run in checkroot, which will need the keyboard to log in...
  266.     # This code was borrowed from the keymap.sh script of console-common
  267.     # Copyright ¬© 2001 Yann Dirson
  268.     # Copyright ¬© 2001 Alcove http://www.alcove.fr/
  269.     if [ "$save_only" != yes ] && \
  270.        [ -x /sbin/sysctl ] && [ -r /etc/sysctl.conf ]; then
  271.     if grep -v '^\#' /etc/sysctl.conf | grep -q keycodes ; then
  272.         grep keycodes /etc/sysctl.conf | grep -v "^#" \
  273.         | while read d ; do
  274.               /sbin/sysctl -w $d 2> /dev/null || true
  275.               done
  276.     fi
  277.     fi
  278.     
  279.     if [ "$save_only" != yes ]; then
  280.     for console in $ACTIVE_CONSOLES; do
  281.         [ -w $console ] || continue
  282.         if which kbd_mode >/dev/null; then
  283.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM" ]; then
  284.             kbd_mode -u <$console
  285.         else
  286.             kbd_mode -a <$console
  287.         fi
  288.         fi
  289.     done
  290.     fi
  291.     
  292.     if which loadkeys >/dev/null; then
  293.     if [ "$XKBMODEL" != SKIP ] && which ckbcomp >/dev/null; then
  294.         if [ "$CHARMAP" != UTF-8 ]; then
  295.         acm_option="-charmap $CHARMAP"
  296.         else
  297.         acm_option=''
  298.         fi
  299.  
  300.         if [ "$XKBRULES" ]; then
  301.         rules_option="-rules $XKBRULES"
  302.         else
  303.         rules_option=''
  304.         fi
  305.  
  306.         if [ "$save_only" != yes ]; then
  307.         if \
  308.             [ "$VARIANT" = '' ] && [ "$CONFIG" != "$USER_CONFIG" ] \
  309.             && [ -f /etc/console-setup/boottime.kmap.gz ] \
  310.             && [ ! /etc/console-setup/boottime.kmap.gz \
  311.                    -ot /etc/default/console-setup ]
  312.         then
  313.             eval loadkeys /etc/console-setup/boottime.kmap.gz $verbose
  314.         else
  315.             ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  316.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  317.             | eval loadkeys $verbose
  318.         fi
  319.         fi
  320.         if which gzip >/dev/null && [ "$save" = yes ]; then
  321.         ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  322.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  323.             | gzip -9 >/etc/console-setup/boottime.kmap.gz
  324.         fi
  325.     elif [ "$save_only" != yes ] && \
  326.          [ -f /etc/console-setup/boottime.kmap.gz ]; then
  327.         eval loadkeys /etc/console-setup/boottime.kmap.gz $verbose
  328.     fi
  329.     fi
  330. fi
  331.  
  332.